package nero.yncc.com.createart.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.bean.BeanGoodChangjing;
import nero.yncc.com.createart.bean.GodBean;
import nero.yncc.com.createart.custom.IconView;
import nero.yncc.com.createart.custom.MyRecyclerView;
import nero.yncc.com.createart.interfaces.InterfaceDoSomeThing;
import nero.yncc.com.createart.utils.ImageUtils;
import nero.yncc.com.createart.utils.PopGuigeChangjing;
import nero.yncc.com.createart.utils.TextUtil;
import nero.yncc.com.createart.utils.ToastUtil;

/**
 * Created by nero on 2017/7/10.
 */

public class AdapterChoosePopChangjing extends BaseAdapter{
    Context context;
    ArrayList<BeanGoodChangjing> arrayList;
    PopGuigeChangjing popGuigeChangjing;

    public AdapterChoosePopChangjing(Context context, ArrayList<BeanGoodChangjing> arrayList, PopGuigeChangjing popGuigeChangjing) {
        this.context = context;
        this.arrayList = arrayList;
        this.popGuigeChangjing = popGuigeChangjing;
    }


    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final Holder holder;
        if (view == null) {
            view = LinearLayout.inflate(context, R.layout.adapter_pop_changjing, null);
            holder = new Holder(view);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }
        final BeanGoodChangjing beanGoodChangjing = arrayList.get(i);
        final BeanGoodChangjing.Goods goods = arrayList.get(i).getGoods();

        ImageUtils.BindImage(context, holder.iv_image, goods.getOriginal_img(), 0);
        holder.tv_title.setText(TextUtil.getText(goods.getGoods_name(), "暂无名称"));
        holder.tv_content.setText(TextUtil.getText(goods.getGoods_content(), "暂无简介"));
        holder.tv_money.setText("价格: ￥" + TextUtil.getText(goods.getShop_price(), "0"));
        holder.tv_count.setText("剩余："+goods.getStore_count());
//        tv_num.setText(TextUtil.getText(goods.getNum() + "", "0"));
        holder.tv_cut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int numCache = beanGoodChangjing.getNum();
                if (numCache > 0) {
                    numCache--;
                    beanGoodChangjing.setNum(numCache);
                    holder.tv_num.setText(TextUtil.getText(numCache + "", "0"));
                    holder.tv_money.setText("价格：￥" + (Double.parseDouble(goods.getShop_price()) * numCache) + "");
                }

            }
        });

        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int numCache = beanGoodChangjing.getNum();
                try {
                    if (numCache < Integer.parseInt(goods.getStore_count())) {
                        numCache++;
                        beanGoodChangjing.setNum(numCache);
                        holder.tv_num.setText(TextUtil.getText(numCache + "", "0"));
                        holder.tv_money.setText("价格：￥" + (Double.parseDouble(goods.getShop_price()) * numCache) + "");
                    } else {
                        Toast.makeText(context, "库存不足，已是最大库存", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Toast.makeText(context, "该商品库存不足，无法下单", Toast.LENGTH_SHORT).show();
                }


            }
        });
        final ArrayList<GodBean.SpecAttr> arrayList_spec = goods.getGoods_spec_list();
        String spec_price = beanGoodChangjing.getSpec_goods_price();
        JSONObject jsonObjectPrice = null;
        try {
            jsonObjectPrice = new JSONObject(spec_price);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final JSONObject finalJsonObjectPrice = jsonObjectPrice;
        AdapterGoodsSpec adapterGoodsSpec = new AdapterGoodsSpec(context, arrayList_spec, new InterfaceDoSomeThing() {
            @Override
            public void doSomething() {
                //循环属性
                String ids = "";
                for (GodBean.SpecAttr specAttr : arrayList_spec) {
                    if (!TextUtils.isEmpty(specAttr.getChooseAttrId())) {
                        ids += specAttr.getChooseAttrId() + "_";
                    }else{
                        return;
                    }
                }
                if (ids.length() > 0) {
                    ids=ids.substring(0, ids.length() - 1);
                }

                try {
                    JSONObject jsonObject_price = finalJsonObjectPrice.getJSONObject(ids);
                    goods.setShop_price(jsonObject_price.getString("price"));
                    goods.setMarket_price(jsonObject_price.getString("market_price"));
                    goods.setStore_count(jsonObject_price.getString("store_count"));
                    AdapterChoosePopChangjing.this.notifyDataSetChanged();
                } catch (Exception e) {
                }

            }
        });
        holder.tv_num.setText(beanGoodChangjing.getNum()+"");
        holder.lv_list_guige.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        holder.lv_list_guige.setAdapter(adapterGoodsSpec);

        if (beanGoodChangjing.isChoose()) {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.project_yellow));
        } else {
            holder.icon_choose.setTextColor(context.getResources().getColor(R.color.textColorGray));
        }
        holder.tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                arrayList.remove(i);
                AdapterChoosePopChangjing.this.notifyDataSetChanged();
            }
        });
        holder.icon_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(beanGoodChangjing.isChoose()){
                    beanGoodChangjing.setChoose(false);
                    AdapterChoosePopChangjing.this.notifyDataSetChanged();
                    popGuigeChangjing.setGuigeMoney();
                    return;
                }
                if (arrayList_spec == null || arrayList_spec.size() == 0) {

                } else {
                    //有规格 必须选择所有规格
                    for (GodBean.SpecAttr specattr : arrayList_spec) {
                        if (TextUtils.isEmpty(specattr.getChooseAttrId())) {
                            ToastUtil.showToast(context, "请先选择" + specattr.getName());
                            return;
                        }
                    }

                }
                //判断数量
                if (beanGoodChangjing.getNum() <= 0) {
                    ToastUtil.showToast(context, "请选择数量");
                    return;
                }
                beanGoodChangjing.setChoose(true);
                AdapterChoosePopChangjing.this.notifyDataSetChanged();
                popGuigeChangjing.setGuigeMoney();
            }
        });
//        SomeUtil.setListViewHeightBasedOnChildren(holder.lv_list_guige,60);
        return view;
    }


    class Holder {
        ImageView iv_image;
        TextView tv_title, tv_content, tv_cut, tv_num, tv_add, tv_money, tv_please_choose,tv_count,tv_close;
        MyRecyclerView lv_list_guige;
        IconView icon_choose;

        public Holder(View view_root) {
            iv_image = view_root.findViewById(R.id.iv_image);
            tv_title = view_root.findViewById(R.id.tv_title);
            tv_content = view_root.findViewById(R.id.tv_content);
            lv_list_guige = view_root.findViewById(R.id.lv_list_guige);
            tv_cut = view_root.findViewById(R.id.tv_cut);
            tv_num = view_root.findViewById(R.id.tv_num);
            tv_add = view_root.findViewById(R.id.tv_add);
            tv_money = view_root.findViewById(R.id.tv_money);
            tv_please_choose = view_root.findViewById(R.id.tv_please_choose);
            icon_choose = view_root.findViewById(R.id.icon_choose);
            tv_count= view_root.findViewById(R.id.tv_count);
            tv_close= view_root.findViewById(R.id.tv_close);
        }
    }
}
